#include "stdafx.h"
#include "wf_modifymirror.h"

#include "pf_snapper.h"



WF_ModifyMirror::WF_ModifyMirror(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Mirror Entities",
                           container, graphicView) {}

QAction* WF_ModifyMirror::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Mirror"
    QAction* action = new QAction(tr("&Mirror"), NULL);
	action->setIcon(QIcon(":/extui/modifymirror.png"));
    //action->zetStatusTip(tr("Mirror Entities"));
    return action;
}


void WF_ModifyMirror::init(int status) {
    PF_ActionInterface::init(status);
}



void WF_ModifyMirror::trigger() {

    PF_DEBUG->print("PF_ActionModifyMirror::trigger()");

    PF_Modification m(*container, graphicView);
    m.mirror(data);

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
}



void WF_ModifyMirror::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyMirror::mouseMoveEvent begin");

    if (getStatus()==SetAxisPoint1 ||
            getStatus()==SetAxisPoint2) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetAxisPoint1:
            axisPoint1 = mouse;
            break;

        case SetAxisPoint2:
            if (axisPoint1.valid) {
                axisPoint2 = mouse;

                deletePreview();
                preview->addSelectionFrom(*container);
                preview->mirror(axisPoint1, axisPoint2);

                preview->addEntity(new PF_Line(preview,
                                               PF_LineAttribute(axisPoint1,
                                                           axisPoint2)));

                drawPreview();
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionModifyMirror::mouseMoveEvent end");
}



void WF_ModifyMirror::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);

    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_ModifyMirror::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

        switch (getStatus()) {
        case SetAxisPoint1:
            axisPoint1 = mouse;
            setStatus(SetAxisPoint2);
        	graphicView->moveRelativeZero(mouse);
            break;

        case SetAxisPoint2:
            axisPoint2 = mouse;
            setStatus(ShowDialog);
        	graphicView->moveRelativeZero(mouse);
            if (PF_DIALOGFACTORY!=NULL) {
                if (PF_DIALOGFACTORY->requestMirrorDialog(data)) {
                    data.axisPoint1 = axisPoint1;
                    data.axisPoint2 = axisPoint2;
                    deletePreview();
                    trigger();
                    finish();
                }
            }
            break;

        default:
            break;
        }
}



void WF_ModifyMirror::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
            /*case Select:
                PF_DIALOGFACTORY->updateMouseWidget(tr("Pick entities to move"),
                                               tr("Cancel"));
                break;*/
        case SetAxisPoint1:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify first point of mirror line"),
                tr("Cancel"));
            break;
        case SetAxisPoint2:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify second point of mirror line"),
                tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_ModifyMirror::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyMirror::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetAxisPoint1:
        case SetAxisPoint2:
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
            break;
        default:
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
            break;
        }
    }
}


// EOF
